/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Oct 16, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.animation;

import edu.psu.geovista.app.pcp.toolkits.collection.ArrayUtils;
import edu.psu.geovista.app.pcp.toolkits.collection.CollectionUtils;
import edu.psu.geovista.classification.Category;
import edu.psu.geovista.classification.CategoryList;
import edu.psu.geovista.data.model.AppDataModel;

import java.util.HashSet;

public class DataProcessor {
   AppDataModel model;
   CategoryList ctgList;


   /**
    * Sort column data in ascending order, return the id of the data
    * @param colName
    * @return
    */
   public int[] getIndexOfSortedData(String colName){
       int colIndex=this.model.getColumnIndexByName(colName);
       float[] values=model.getColumnValueAsFloat(colIndex);
       return ArrayUtils.getIndexOfSortedValue(values);
   }

   public int[] getIndexOfSortCategoryData(int[] categoryIds,String colName) {
       int colIndex=this.model.getColumnIndexByName(colName);
       float[] values=model.getColumnValueAsFloat(colIndex);

       int[] Ids=new int[values.length ];
       int count=0; //total number of categorys' member id

       for (int i=0;i<categoryIds.length ;i++){
          Category ctg=ctgList.getCategoryByID(categoryIds[i]) ;
          HashSet IDs=ctg.getMemberIds() ;
          int[] ctgids=CollectionUtils.convertToIntArray(IDs);
          int[] sortids=ArrayUtils.getIndexOfSortedValue(values,ctgids);
          System.arraycopy(sortids,0,Ids,count,sortids.length );
           count=count+sortids.length ;

       }
       int[] finalIds=new int[count];
       System.arraycopy(Ids,0,finalIds,0,count);
       return finalIds;

   }
   /**
    *
    * @param selectedIds
    * @param colName
    * @return    index of sorted data value
    */
   public int[] getIndexOfSelectedData(int[] selectedIds ,String colName) {
       int colIndex=this.model.getColumnIndexByName(colName);
       float[] values=model.getColumnValueAsFloat(colIndex);

       return ArrayUtils.getIndexOfSortedValue(values,selectedIds);

   }

    public void setModel(AppDataModel model) {
        this.model = model;
    }

    public void setCtgList(CategoryList ctgList) {
        this.ctgList = ctgList;
    }
}
